<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ThinkYXC-CMS系统-友情链接管理</title>
    <link rel="stylesheet" type="text/css" href="../layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="../css/style.css">
    
</head>

<body>
    <div class="layui-card">
  <!--<div class="layui-card-header">卡片面板</div>-->
  <div class="layui-card-body">
     <div class="layui-tab">
  <ul class="layui-tab-title tabl">
    <li class="layui-this">所有友情链接</li>
    <li>添加友情链接</li>
  </ul>
  <div class="layui-tab-content">
    <div class="layui-tab-item layui-show">
        <!--内容1-->
        <div class="layui-card">
  <!--<div class="layui-card-header">卡片面板</div>-->
  <div class="layui-card-body">
      <table class="layui-hide" id="test" lay-filter="test"></table>
      
      <script type="text/html" id="barDemos">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="statusp">

{{#  if(d.status == 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="status">已发布</a>
  {{#  } else { }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="status">未发布</a>
  {{#  } }}
</script>
<script type="text/html" id="thumbnail">

{{#  if(d.thumbnail){ }}
    <a class="layui-btn layui-btn-xs" lay-event="thumbnail">有</a>
  {{#  } else { }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="thumbnail">无</a>
  {{#  } }}
</script>
  </div>
</div>

        
    </div>
    
    <!--内容2-->
    <div class="layui-tab-item">
        
       <form class="layui-form forms" action="" lay-filter="nei2s"> 
            <div class="layui-form-item">
    <label class="layui-form-label">名称<span class="bitian">*</span></label>
    <div class="layui-input-block">
      <input type="text" lay-verify="required" lay-reqtext="信息是必填项，岂能为空？" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
   <div class="layui-form-item">
    <label class="layui-form-label">链接地址<span class="bitian">*</span></label>
    <div class="layui-input-block">
      <input type="url" lay-verify="required" lay-reqtext="信息是必填项，岂能为空？" name="url" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
    <div class="layui-form-item">
    <label class="layui-form-label"><span class="bitian"></span>缩略图</label>
    <div class="layui-input-block">
      <div class="layui-upload">
  <button type="button" class="layui-btn" id="upimag">上传图片</button>
  <div class="layui-upload-list">
    <img class="layui-upload-img" id="images">
    <p id="demoText"></p>
  </div>
</div>
    </div>
  </div>
     <div class="layui-form-item">
    <label class="layui-form-label widthauto">打开方式</label>
    <div class="layui-input-block">
      <select name="target" lay-filter="aihao">
        <option value="_blank">新标签页打开</option>
        <option value="_self">本窗口打开</option>
      </select>
    </div>
  </div> 
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit="" lay-filter="nei2">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
      </form>  
    </div>
  </div>
</div>
  </div>
</div>


 
 <script type="text/javascript" src="../layui/layui.all.js"></script>
<script>

var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,$ = layui.jquery
  ,upload = layui.upload
  ,laydate = layui.laydate
  ,table = layui.table;
  var laytpl = layui.laytpl;
  var util = layui.util
  ,  active = {
    getCheckData: function(){ //获取选中数据
      var checkStatus = table.checkStatus('test')
      ,data = checkStatus.data;
      if(!data[0]){
          layer.msg('请选中要删除的内容');
          return
      }
      layer.confirm('真的删除么？', function(index){
                //do something
        //   layer.alert(JSON.stringify(data));
              $.ajax({
                  url:"{:url('link/dellist')}",
                  data:{'data':JSON.stringify(data)},
                  type:"Post",
                  dataType:"json",
                  success:function(data){
                    console.log(data);
                    if(data.code == 1){
                         updata();
                     }
                      layer.msg(data.msg);
                    
                  },
                  error:function(data){
                      $.messager.alert('错误',data.msg);
                  }
              });    
        
        layer.close(index);
}); 
      
    }
    ,getCheckLength: function(){ //获取选中数目
      var checkStatus = table.checkStatus('test')
      ,data = checkStatus.data;
      layer.msg('选中了：'+ data.length + ' 个');
    }
    ,isAll: function(){ //验证是否全选
      var checkStatus = table.checkStatus('test');
      layer.msg(checkStatus.isAll ? '全选': '未全选')
    }
  };
  
    $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
    //温馨提示：默认由前端自动合计当前行数据。从 layui 2.5.6 开始： 若接口直接返回了合计行数据，则优先读取接口合计行数据。
  //详见：https://www.layui.com/doc/modules/table.html#totalRow
  table.render({
    elem: '#test'
    ,url:'/admin/link/lists'
    ,toolbar: '#toolbarDemo'
    ,title: '文章数据'
    // ,totalRow: true
    ,cols: [[
      {type: 'checkbox', fixed: 'left',totalRowText: '合计',}
      ,{field:'weight', title:'排序', sort: true, totalRow: true, align:'center'}
      ,{field:'linkid', title:'友情链接ID', sort: true, totalRow: true, align:'center'}
      ,{field:'name', title:'链接名称',  align:'center'}
      ,{field:'url', title:'链接地址',  totalRow: true, align:'center'}
      ,{field:'create_date', title:'发布时间', align:'center',templet: function(d){
        return util.toDateString(d.create_date*1000)
      }}
      ,{field:'thumbnail', title:'图标',toolbar: '#thumbnail', align:'center'}
      ,{field:'status', title:'状态',toolbar: '#statusp', align:'center'}
      ,{fixed: 'operate', title:'操作', toolbar: '#barDemos', align:'center',fixed: 'right' }
    ]]
    ,page: true
     ,response:{
		      statusName:'code', //规定返回的状态码字段为code
			  statusCode:1 //规定成功的状态码味200
    }
    ,parseData: function(res){ //res 即为原始返回的数据
    return {
      "code": res.code, //解析接口状态
      "msg": res.msg, //解析提示文本
      "count": res.count, //解析数据长度
      "data": res.data  //解析数据列表
    };
  }
    ,done: function(res, curr, count){
    //如果是异步请求数据方式，res即为你接口返回的信息。
    //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
    console.log(res.code);
    
    //得到当前页码
    console.log(curr); 
    
    //得到数据总量
    console.log(count);
  }
  });
  
      //重新渲染数据
    function updata(){
        table.reload('test', {
                  url: '/admin/link/lists'
        });
        
    }
  
  form.on('submit(nei2)', function(data){
        var dast = form.val("nei2s");
        console.log(dast)
        $.ajax({
          url:"{:url('link/add')}",
          data:{'data':dast,'thumbnail':image},
          type:"Post",
          dataType:"json",
          success:function(data){
              layer.msg(data.msg);
              if(data.code == 1){
                  setTimeout(function(){
                  location.reload(); //删除成功后再刷新
                // layer.closeAll();
              },1000)
              }
          },
          error:function(data){
              $.messager.alert('错误',data.msg);
          }
      });
    // layer.alert(data.field.interest, {
    //   title: '最终的提交信息'
    // })
    return false;
  });
  
  
       var image;
   //普通图片上传
  var uploadInst = upload.render({
    elem: '#upimag'
    ,url: "{:url('link/image')}" //改成您自己的上传接口
    ,before: function(obj){
        //上传前的回调
    //   //预读本地文件示例，不支持ie8
    //   obj.preview(function(index, file, result){
    //     // $('#images').attr('src', result); //图片链接（base64）
    //     console.log(file)
    //      console.log(index)
    //   });
    }
    ,done: function(res){
        //上传后的回调
        console.log(res)
        image = res.image;
        $('#images').attr('src', res.image);
      //如果上传失败
      if(res.code == 0){
        return layer.msg('上传失败');
      }
      //上传成功
    }
    ,error: function(){
      //演示失败状态，并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });
  
   //监听工具条 
table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
  var data = obj.data; //获得当前行数据
  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
  var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
    console.log(data)
 
  if(layEvent === 'detail'){ //查看
    //do somehing
    
    
  } else if(layEvent === 'del'){ //删除
    layer.confirm('真的删除么？', function(index){
      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
      layer.close(index);
      //向服务端发送删除指令
       $.ajax({
          url:"{:url('link/del')}",
          data:{'linkid':data.linkid},
          type:"Post",
          dataType:"json",
          success:function(data){
            console.log(data);
            if(data.code == 1){
                setTimeout(function(){
                    updata();
                },1000)
                //  updata();
             }
              layer.msg(data.msg);
            
          },
          error:function(data){
              $.messager.alert('错误',data.msg);
          }
      });
      
    });
  } else if(layEvent === 'edit'){ //编辑
    //do something
            layer.open({
  type: 2,
  area: ['100%', '100%'],
  content: '/admin/linkedit?linkid='+2,
  zIndex: layer.zIndex, //重点1
  success: function(layero){
    layer.setTop(layero); //重点2
  } ,cancel: function(){ 
    //右上角关闭回调
    setTimeout(function(){
        updata();
                // layer.closeAll();
    },1000)
   
    //return false 开启该代码可禁止点击该按钮关闭
  }
}); 
    
  } else if(layEvent === 'status'){ //编辑
    //do something
    
        //未发布改为已发布
        //已发布改为未发布
       $.ajax({
          url:"{:url('link/status')}",
          data:{'linkid':data.linkid,'status':data.status},
          type:"Post",
          dataType:"json",
          success:function(data){
            console.log(data.code);
             if(data.code == 1){
                 updata();
             }
              layer.msg(data.msg);
              
          },
          error:function(data){
              $.messager.alert('错误',data.msg);
          }
      });
        

    
    
  } else if(layEvent === 'LAYTABLE_TIPS'){
      
    layer.alert('Hi，头部工具栏扩展的右侧图标。');
  }
});
  
 
   </script>
</body>
</html>